package com.hzmg.akkacloud.workdemo;

import com.google.common.collect.Maps;
import com.hzmg.akkacloud.common.model.WorkerInfo;
import com.hzmg.akkacloud.worker.background.ReactiveDiscoveryClient;
import com.hzmg.akkacloud.worker.common.utils.RemoteInvokeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Map;


/**
 * Demo启动类
 *
 * @author zbw
 * @since 2022/7/29
 */
@SpringBootApplication
@Slf4j
public class WorkDemoApplication2 {
    public static void main(String[] args) {
        System.setProperty("server.port", "8888");
        System.setProperty("akkacloud.worker.akka-port", "2222");
        SpringApplication.run(WorkDemoApplication2.class, args);

        try {
            /*log.info("I am sleep.");
            Thread.sleep(20000);
            log.info("I am wake up..");*/
            ReactiveDiscoveryClient reactiveDiscoveryClient = new ReactiveDiscoveryClient();
            reactiveDiscoveryClient.runWhenInstanceIsReceive(() -> {
                test();
                test();
                test();
                test();
                test();
                test2();
                test3();
                test4();
                test4();
                test4();
                test4();
                test4();
                test4();
                test5();
                test6();
            });
            /*test();*/
            log.info("I am first.");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static void test() {
        log.info("test run");
        Map<Integer, String> map = Maps.newHashMap();
        map.putIfAbsent(1, "test");
        RemoteInvokeUtils.getRemoteService(map, "simple", "akkcloud-client-demo");
    }

    public static void test2() {
        log.info("test2 run");
        Map<Integer, String> map = Maps.newHashMap();
        map.putIfAbsent(1, "test");
        RemoteInvokeUtils.getRemoteService(map, "simple2", "akkcloud-client-demo");
    }

    public static void test3() {
        log.info("test3 run");
        WorkerInfo workerInfo = new WorkerInfo();
        workerInfo.setAkkaAddress("1111");
        WorkerInfo resultMap = RemoteInvokeUtils.getRemoteServiceResult(workerInfo, "simple3", "akkcloud-client-demo");
        log.info(resultMap.toString());
    }

    public static void test4() {
        log.info("test4 run");
        WorkerInfo workerInfo = ReactiveDiscoveryClient.getAllAliveWorkers("akkcloud-client-demo").get(0);
        RemoteInvokeUtils.getRemoteService(workerInfo, "simple4", "akkcloud-client-demo");
    }

    public static void test5() {
        log.info("test5 run");
        RemoteInvokeUtils.getRemoteService(null, "simple5", "akkcloud-client-demo");
    }

    public static void test6() {
        log.info("test6 run");
        WorkerInfo workerInfo = RemoteInvokeUtils.getRemoteServiceResult(null, "simple6", "akkcloud-client-demo");
        log.info(workerInfo.toString());
    }
}
